﻿<?php
/*
 * kkFileView文件文档在线预览
 *@link        http://www.dzzoffice.com
* @author      小胡
 *@QQ:         3203164629
 *@time:       20240716
 */

if (!defined('IN_OAOOA')) {//所有的php文件必须加上此句，防止被外部调用
	exit('Access Denied');
}
try {
    $appdata = C::t('setting')->fetch('setting_kkFileViewDataSetting',true);
	if ($appdata) {
		$host = $appdata['host'];
		$watermark = $appdata['watermarkTxt'];
		$status = $appdata['status'];
	} else {
		showmessage(lang('configuration_information_is_incomplete'));
		exit();
	}
	if ($status) {
		if (!$host) {
			showmessage(lang('kkFileView_interface_is_empty'));
			exit();
		}
		if (isset($_GET['path'])) {
			if (!$rid = dzzdecode($_GET['path'],'',0)) {
				exit('Access Denied');
			}
			$meta = IO::getMeta($rid);
			if (!$meta) {
				showmessage(lang('file_not_exist'));
				exit();
			}
			$navtitle = $meta['name'];
			$stream = getglobal('siteurl') . 'index.php?mod=io&op=getStream'.'&path=' . dzzencode($rid.'_3', '', 0, 0);
			$url=getglobal('siteurl') . 'index.php?mod=io&op=getStream'.'&path=' . dzzencode($rid.'_3', '', 0, 0).'&name=/'.$meta['name'];
			$previewUrl = $url . '&fullfilename=' . $meta['name'];
			$encodedPreviewUrl = rawurlencode(base64_encode($previewUrl));
			if ($watermark) {
				$watermark = '&watermarkTxt=' . rawurlencode(watermark($watermark,$meta['name']));
			}
			$finalUrl = $host . '/onlinePreview?url=' . $encodedPreviewUrl . $watermark;
			header('Location:' . $finalUrl);
		}
	} else {
		showmessage(lang('kkFileView_is_not_enabled'));
		exit();
	}
} catch (Exception $e) {
	showmessage($e->getMessage());
	exit();
}
function watermark($string,$name, $replaces = array()){
    global $_G;
    $sitevars = array(
        '{sitename}' => $_G['setting']['sitename'],
        '{version}' => CORE_VERSION,
        '{years}' =>  date("Y"),
        '{time}' => dgmdate(TIMESTAMP, 'Y-n-j H:i'),
        '{adminemail}' => $_G['setting']['adminemail'],
        '{username}' => $_G['member']['username'],
        '{title}' => $name
    );
    $replaces = array_merge($sitevars, $replaces);
    return str_replace(array_keys($replaces), array_values($replaces), $string);
}